/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.com.cn / www.i-gray.cn
 *
 *	[ WARNING!!! ] This is a freeware!
 *	[ WARNING!!! ] But, You MUST get author's authorization before using it!
 *
 *	Name : dhtml.js [ For MDK ] / Development Code Evolve
 *	Created / Modify : 2009-9-25 / 2009-12-23
 */
if( is_undefined( MDK ) || !is_object( MDK ) )
	alert( '运行dhtml.js需要 [ MDK 库 ]...' );

//	[CH]	创建dhtml对象, 继承至MDK
var	dhtml	=	Class
({
	Version	:		'1.0.1',
	Author	:		'gray',
	Name	:		'dhtml',

	getClientTop	:	function ()
	{
		if( !is_undefined( self.pageYOffset ) )
			return	self.pageYOffset;

		if( document.documentElement && document.documentElement.scrollTop )
			return	document.documentElement.scrollTop;

		if( document.body.scrollTop )
			return	document.body.scrollTop;

		return	0;
	},

	getClientLeft	:	function ()
	{
		if( !is_undefined( self.pageXOffset ) )
			return	self.pageXOffset;

		if( document.documentElement && document.documentElement.scrollLeft )
			return	document.documentElement.scrollLeft;

		if( document.body.scrollLeft )
			return	document.body.scrollLeft;

		return	0;
	},

	getClientWidth	:	function ()
	{
		if( self.innerWidth )
			return	self.innerWidth;

		if( document.documentElement && document.documentElement.clientWidth )
			return	document.documentElement.clientWidth;

		if( document.body )
			return	document.body.clientWidth;

		return	0;
	},

	getClientHeight :	function ()
	{
		if( self.innerHeight )
			return	self.innerHeight;

		if( document.documentElement && document.documentElement.clientHeight )
			return	document.documentElement.clientHeight;

		if( document.body )
			return	document.body.clientHeight;

		return	0;
	},

	getPageHeight	:	function ()
	{
		var yScroll, windowHeight;

		if( window.innerHeight && window.scrollMaxY )
		{
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if ( document.body.scrollHeight > document.body.offsetHeight )
		{
			yScroll = document.body.scrollHeight;
		}
		else
		{
			yScroll = document.body.offsetHeight;
		}

		if( self.innerHeight )
		{
			windowHeight = self.innerHeight;
		}
		else if( document.documentElement && document.documentElement.clientHeight )
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else if( document.body )
		{
			windowHeight = document.body.clientHeight;
		}

		if( yScroll < windowHeight )
		{
			pageHeight = windowHeight;
		}
		else
		{
			pageHeight = yScroll;
		}

		yScroll	= windowHeight	=	null;

		return	pageHeight;
	},

	getPageWidth	:	function ()
	{
		var xScroll, windowWidth;

		if( window.innerWidth && window.scrollMaxX )
		{
			xScroll = window.innerWidth + window.scrollMaxX;
		}
		else if ( document.body.scrollWidth > document.body.offsetWidth )
		{
			xScroll = document.body.scrollWidth;
		}
		else
		{
			xScroll = document.body.offsetWidth;
		}

		if( self.innerWidth )
		{
			windowWidth = self.innerWidth;
		}
		else if( document.documentElement && document.documentElement.clientWidth )
		{
			windowWidth = document.documentElement.clientWidth;
		}
		else if( document.body )
		{
			windowWidth = document.body.clientWidth;
		}

		if( xScroll < windowWidth )
		{
			pageWidth = windowWidth;
		}
		else
		{
			pageWidth = xScroll;
		}

		xScroll	= windowWidth	=	null;

		return	pageWidth;
	},

	create	:	function ( tags, id, parent, style, innerHTML )
	{
		var	oElement	=	document.createElement( tags );

		if( id && typeof id == 'object' )
			this.bind( oElement, id );
		else if( id && typeof id == "string" )
			oElement.id		=	id;

		if( style && typeof style == 'object' )
			this.setStyle( oElement, style );
		else if( style && typeof style == 'string' )
			oElement.className	=	style;

		if( innerHTML )
			oElement.innerHTML	=	innerHTML;

		if( !parent )
			document.body.appendChild( oElement );
		else
			parent.appendChild( oElement );

		return	oElement;
	},

	setStyle	:	function ( oElement, styles )
	{
		for( var x in styles )
		{
			if( x == 'opacity' )
			{
				if( MDK.Browser.name == 'IE' )
					oElement.style.filter	=	'Alpha(opacity=' + styles[x] * 100 + ')';
				else
					oElement.style.opacity	=	styles[x];
			}
			else
			{
				oElement.style[x]	=	styles[x];
			}
		}
	},

	getStyle	:	function ( elm, x )
	{
		var	style	= ( x == 'float' || x == 'cssFloat' ) ? 'styleFloat' : x;
		var value	= elm.style[style];
		if( !value && elm.currentStyle )
			value	= elm.currentStyle[style];

		if( value == 'auto' || !value )
		{
			if( ( style == 'width' || style == 'height' ) && ( this.getStyle( elm, 'display' ) != 'none' ) )
			{
				return	elm['offset' + ( style.toLowerCase() == 'width' ? 'Width' : 'Height' ) ] + 'px';
			}
			return	null;
		}
		return	value;
	},

	bind		:	function ( oElement, attr )
	{
		if( attr && typeof attr == 'object' )
		{
			for( var x in attr )
			{
				oElement.setAttribute( x, attr[x] );
			}
		}
	},

	remove	:	function ( obj, parent )
	{
		//var	$A	=	MDK.Ajax;
		var	parent	=	parent ? parent : document.body;
		if( MDK.Ajax.xmlHttp )
					MDK.Ajax.xmlHttp.abort();

		/**
		 *	这里需要对闭包造成的内存泄露进行处理
		 *
		 *	需要对IE进行彻底的优化
		 */

		this.GC( obj )
		parent.removeChild( obj );
		/*$A	=	*/parent	=	/*obj	=*/	null;
	},

	GC			:	function ( obj )
	{
		var	att	=	obj.attributes;
		var	nodes	=	obj.childNodes;

		if( nodes )
		{
			for( var i = 0, j = nodes.length; i < j; i++ )
				this.GC( obj.childNodes[i] );
		}

		if( att )
		{
			for( var i = 0, j = att.length; i < j; i++ )
			{
				if( typeof obj[att[i].name] === 'function' )
					obj[att[i].name] = null;
			}
		}

		obj	=	att	=	nodes	=	null;
	}
}, MDK );

dhtml.prototype.getFront	=	function ( elm, obj )
	{
		var	z	=	0;
		var	obj	=	obj ? obj : 'div';

		var	TempArr	=	$$( obj );
		for( var i = 0, j = TempArr.length; i < j; i++ )
		{
			var	zIndex	=	parseInt( TempArr[i].style.zIndex ) ? parseInt( TempArr[i].style.zIndex ) : null;
			if( zIndex == '' || zIndex == null )
				continue;
			else
				z	=	zIndex;
		}

		elm.style.zIndex	=	++z;
		z = obj = TempArr = zIndex = null;
	}

dhtml.prototype.drag	=	function ( elm, e, obj )
{
	var	e	=	e || window.event;
	var	elm	=	!obj ? elm : obj;
	var	z	=	0;

	elm.onselectstart	=	function (e) { return false };
	elm.style.position	=	'absolute';
	if( e.button == 2 )	return;

	//	[CH]	获取坐标参数
	var	startX = e.clientX, startY = e.clientY;
	var	originalX = elm.offsetLeft, originalY = elm.offsetTop;
	var deltaX = startX - originalX, deltaY = startY - originalY;

	addEventHandle( document, 'mousemove', moveHandler );
	addEventHandle( document, 'mouseup', upHandler );

	if( e.stopPropagation )
		e.stopPropagation();
	else
		e.cancelBubble	=	true;

	if( e.preventDefault )
		e.preventDefault();
	else
		e.returnValue	=	false;

	function	moveHandler ( e )
	{
		elm.style.left	=	( e.clientX - deltaX ) + 'px';
		elm.style.top	=	( e.clientY - deltaY ) + 'px';

		if( e.stopPropagation )
			e.stopPropagation();
		else
			e.cancelBubble	=	true;
	}

	function	upHandler ( e )
	{
		removeEventHandle( document, 'losecapture', upHandler );
		removeEventHandle( document, 'mouseup', upHandler );
		removeEventHandle( document, 'mousemove', moveHandler );
		startX	=	startY	=	originalX	=	originalY	=	deltaX	=	deltaY	=	null;
	}

	if( e.stopPropagation )
		e.stopPropagation();
	else
		e.cancelBubble	=	true;
}

dhtml.prototype.loading	=	function ( string )
{
	if( $( 'MDKloading' ) && $( 'MDKprogress' ) )
	{
		//if( this.getStyle( $( 'MDKloading' ), 'display' ) != 'none' )
			$( 'MDKprogress' ).innerHTML = string;
		//else
			this.setStyle( $( 'MDKloading' ), { display : 'block' } );

		if( MDK.Browser.name == 'IE' )
			$( 'MDKloading' ).style.filter	=	'Alpha(opacity=100)';
		else
			$( 'MDKloading' ).style.opacity	=	'1';
	}
	return;
}

dhtml.prototype.display	=	function ( elm, method )
{
	if( !elm )
		return;

	var	intervalId, timeoutId;
	//	[CH]	防止快速点击造成的延时错误
	clearInterval( intervalId );
	clearTimeout( timeoutId );

	if( !method )
	{
		var	start	=	MDK.Browser.name == 'IE' ? 100 : 1;
		var	end		=	0;
	}
	else
	{
		var	start	=	0;
		var	end		=	MDK.Browser.name == 'IE' ? 100 : 1;
	}

	if( method )
	{
		if( MDK.Browser.name == 'IE' )
		{
			elm.style.filter	=	'alpha(opacity:' + start + ')';
			elm.style.display	=	'block';
		}
		else
		{
			elm.style.opacity	=	start;
			elm.style.display	=	'block';
		}
	}

	function ieElementDisplay ()
	{
		if( start == end )
		{
			clearInterval( intervalId );
			if( !method )
			{
				elm.style.display	=	'none';
			}
			else
			{
				elm.style.filter	=	'alpha(opacity:' + start + ')';
			}
			start = end = timeoutId = intervalId = null;
			return;
		}
		else
		{
			elm.style.filter	=	'alpha(opacity:' + start + ')';
			start	=	!method ? start - 20 : start + 20;
		}
	}

	function	mozElementDisplay ()
	{
		if( parseFloat( start.toString().substr ( 0, 4 ) ) == end )
		{
			clearInterval( intervalId );
			if( !method )
			{
				elm.style.display	=	'none';
			}
			else
			{
				elm.style.opacity	=	start;
			}
			return;
		}
		else
		{
			//	[CH]	Moz 下一个莫名的Bug
			//	[CH]	1 - 0.05 居然会出现等于 0.8499999999999 这样的值
			//	[CH]	-_-!
			//start	=	parseFloat(  );
			elm.style.opacity	=	parseFloat( start.toString().substr( 0, 4 ) );
			start	=	!method ? start - 0.05 : start + 0.05;
		}
	}

	if( MDK.Browser.name == 'IE' )
	{
		timeoutId	=	setTimeout( function (e) { intervalId = setInterval( ieElementDisplay, 50 ) }, 200 );
	}
	else
	{
		timeoutId	=	setTimeout( function (e) { intervalId = setInterval( mozElementDisplay, 50 ) }, 200 );
	}
}